package com.css.fxfzysx.modules.cztDataMange.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.cztDataMange.service.DomeFacilitiesService;
import com.css.fxfzysx.modules.lifeline.param.RegionParam;
import com.css.fxfzysx.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("domeFacilities")
@Slf4j
public class DomeFacilitiesController {

    @Autowired
    private DomeFacilitiesService domeFacilitiesService;

    /**
     * 导入水库大坝
     */
    @PostMapping("parseShp")
    public RestResponse parseShp(@RequestBody RegionParam param) {
        try {
            return domeFacilitiesService.parseShp(param);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 导入重大工程历史震害信息
     *
     * @param file
     * @return
     */
    @PostMapping("importMajorProjects")
    public RestResponse uploadBvInformation(@RequestParam("file") MultipartFile file,
                                            @RequestParam("province") String province,
                                            @RequestParam("city") String city,
                                            @RequestParam("county") String county,
                                            @RequestParam("id") String id,
                                            @RequestParam("cover") Integer cover,
                                            @RequestParam("fileIds") String fileIds) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return domeFacilitiesService.importMajorProjects(file, province, city, county,id,cover,fileIds);
    }

    /**
     * 根据省市区和类型查询
     *
     * @param type
     * @param province
     * @param city
     * @param county
     * @return
     */
    @GetMapping("getByRegionAndType")
    public RestResponse getByRegionAndType(Integer type, String province, String city, String county, HttpServletRequest request) {
        try {
            int page = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            HashMap<String, Object> map = domeFacilitiesService.getByRegionAndType(type, province, city, county, page, size);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get getByRegionAndType error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }


    /**
     * 修改
     *
     * @param map
     * @return
     */
    @PostMapping("updateByTypeAndId")
    public RestResponse updateByTypeAndId(@RequestBody HashMap<Integer, Object> map) {
        try {
            domeFacilitiesService.updateByTypeAndId(map);
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            log.error("post updateByTypeAndId error:{}", e.getMessage(), e);
            return RestResponse.fail("修改失败!");
        }
    }


    /**
     * 删除
     * @param type
     * @param id
     * @return
     */
    @GetMapping("deleteByTypeAndId")
    public RestResponse deleteByTypeAndId(Integer type,String id){
        try {
            domeFacilitiesService.deleteByTypeAndId(type,id);
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            log.error("get deleteByTypeAndId error:{}",e.getMessage(),e);
            return RestResponse.fail("删除失败!");
        }
    }
}
